var userId;
var merchantNo;
$(function () {
	
	var thisURL = document.URL;   
	var getval = thisURL.split('?')[1];  
	userId = (getval.split("=")[1]).split("&")[0];
	//console.log('userId:'+userId);
	merchantNo = getval.split("=")[2];
	//console.log('merchantNo:'+merchantNo);
    $("#jqGrid").jqGrid({
        url: '../qrCode/list',
        datatype: "json",
        postData:{
        	userId:userId
        	},
        colModel: [			
			{ label: '收款码名称', name: 'codeName', index: 'code_name', width: 60 }, 			
			{ label: '收款码编号', name: 'codeNo', index: 'code_no', width: 100 }, 	
			{ label: '开户行名称', name: 'bankName', index: 'b.bank_name', width: 100 }, 
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,20,30],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
  //根据商户号获取银行信息下拉
    $.ajax({
    	type: "POST",
    	data: JSON.stringify(merchantNo),
	    url: "../bankInfo/getBankInfoSelect",
	    success: function(result){
	    	//console.log(result)
	    	if(result.code == 0){
	    		var list = result.options;
		    	for(var i=0;i<list.length;i++){
		    		var obj = {};
		    		obj = list[i];
		    		vm.bankOptions[i] = obj;
		    	}
	    	}
		}
	});
});

function showInfo(userId){
	parent.layer.open({
		  type: 2,
		  title: '二维码列表',
		  shadeClose: true,
		  shade: 0.8,
		  fixed: false, //不固定
		  maxmin: true,
		  area: ['60%', '80%'],
		  //content: 'merchant/listQRCode.html?id='+id //iframe的url
		  content: 'qrCode/getCodeList.do?userId='+userId //iframe的url
		}); 
}

function showInfo(){
	parent.layer.open({
		  type: 2,
		  title: '二维码列表',
		  shadeClose: true,
		  shade: 0.8,
		  fixed: false, //不固定
		  maxmin: true,
		  area: ['60%', '80%'],
		  //content: 'merchant/listQRCode.html?id='+id //iframe的url
		  content: 'cashiers/getCodeList.do?userId='+userId //iframe的url
		}); 
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
		},
		showList: true,
		title: null,
		qrCode: {},
		selected: '',
		bankOptions:[],
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.qrCode = {};
			vm.getInfo(userId);
		},
		
		saveOrUpdate: function (event) {
			vm.qrCode.userId = userId;
			var url ="../qrCode/save";
			$("#submitBtn").attr("disabled","disabled");
			if(vm.qrCode.bankId == null || "" == vm.qrCode.bankId){
				alert('请选择一个银行');
				return ;
			}
			
			if(vm.qrCode.codeName == null || "" == vm.qrCode.codeName){
				alert('收款码名称不能为空');
				return ;
			}
			
			/*if($('#codeName').val() == null || $('#codeName').val() == ''){
				alert('收款码名称不能为空');
				return ;
			}*/
			
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.qrCode),
			    success: function(r){
			    	if(r.code == 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
			    	$("#submitBtn").removeAttr("disabled");
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../qrCode/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		
		getInfo: function(id){
			$.get("../qrCode/info/"+id, function(r){
                vm.qrCode = r.qrCode;
            });
		},
		view: function(id){
			
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			page = 1;
			var keyword = $("#keyword").val();
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{keyword:keyword},
            }).trigger("reloadGrid");
		}
	}
});